package com.ambow.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ambow.entity.Message;
import com.ambow.entity.Post;
import com.ambow.entity.User;
import com.ambow.service.MessageService;
import com.ambow.service.PostService;
import com.ambow.service.impl.MessageServiceImpl;
import com.ambow.service.impl.PostServiceImpl;
import com.ambow.utils.PageResults;
import com.mchange.v2.codegen.intfc.ReflectiveDelegationPolicy;

/**
 * Servlet implementation class Postservlet
 */
@WebServlet("/Postservlet")
public class Postservlet extends HttpServlet {
	MessageService mService = new MessageServiceImpl();
	PostService pServicervice = new PostServiceImpl();
	MessageService messageService = new MessageServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String status = request.getParameter("status");
		System.out.println("dytuyiu" + status);
		if (status.equals("add")) {
			add(request, response);
		} else if (status.equals("sall")) {
			findall(request, response);
		} else if (status.equals("reply")) {
			reply(request, response);
		}

	}

	private void reply(HttpServletRequest request, HttpServletResponse response) {
		try {

			User user = (User) request.getSession().getAttribute("login");
			if (user != null) {
				request.getRequestDispatcher("pages/reply.jsp").forward(request, response);
			}else {
				response.sendRedirect("pages/account.jsp");
			}
		} catch (Exception e) {
		}

	}

	private void findall(HttpServletRequest request, HttpServletResponse response) {
		int id = Integer.valueOf(request.getParameter("id"));// 留言的id
		String hql = "from Post where status=1 and message_id= "+id;
		List<Post> post1 = pServicervice.queryBySql(hql);
		Message message = messageService.queryById(Message.class, id);
		request.setAttribute("post1", post1);
		request.setAttribute("message", message);
		try {
			request.getRequestDispatcher("pages/redisplay.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		int id = Integer.valueOf(request.getParameter("id"));
		Message message = messageService.queryById(Message.class, id);
		Post post = new Post();
		post.setTitle(title);
		post.setCreateTime(new Date());
		post.setContent(content);
		post.setStatus(1);
		post.setMessage(message);
		boolean b = pServicervice.savePost(post);
		if (b) {

			List<Message> messages = mService.selectAllMessage();
			request.setAttribute("messages", messages);
			String sql = "from Post where status=1 and message_id= " + id;
			List<Post> post1 = pServicervice.queryBySql(sql);

			request.setAttribute("post1", post1);
			request.setAttribute("message", message);

			request.getRequestDispatcher("pages/redisplay.jsp").forward(request, response);
		}
	}
}
